//
//  TimeMessage.m
//  uteped_sdk
//
//  Created by mateng on 2024/2/20.
//

#import "TimeMessage.h"
#import "Const.h"

@implementation TimeMessage

- (NSString *)getPath {
    return @"time";
}

- (BOOL) methodCallConsumed:(NSString*)method params:(NSDictionary*)params result:(FlutterResult)result
{
    BOOL consumed = false;
    if ([super methodCallConsumed:method params:params result:result])
        return YES;
    
    assert(method != nil);
    if ([method isEqualToString:M_setTimeClock]) {
        if (![self isConnected]) {
            result(@NO);
            return YES;
        }
        
        [self syncTimeClock:^(NSInteger errorCode, NSDictionary * _Nonnull uteDict) {
            result(errorCode == UTEDeviceErrorNil ? @YES : @NO);
        }];
    }
    else if ([method isEqualToString:M_getTimeClock]) {
        if (![self isConnected]) {
            result(@NO);
            result([NSDictionary dictionary]);
            result([NSArray array]);
            return YES;
        }
        
        [[UTEDeviceMgr sharedInstance] getTimeClock:^(NSInteger timeSeconds, NSInteger timeZone, NSInteger minuteOffset, NSInteger errorCode,NSDictionary *uteDict) {
            if (errorCode == UTEDeviceErrorNil) {
                result([NSDictionary dictionaryWithObjectsAndKeys:
                        @(timeSeconds), @"timeSeconds",
                        @(minuteOffset), @"minuteOffset",
                        @(timeZone),@"timeZone",
                        nil]);
            } else {
                result([NSDictionary dictionary]);
            }
        }];
    }
    else if ([method isEqualToString:M_queryWorldClock]) {
        result(@YES);
    }
    else if ([method isEqualToString:M_querySupportWorldClockCount]) {
        result(@YES);
    }
    else if ([method isEqualToString:M_setWorldClock]) {
        result(@YES);
    }
    else {
        consumed = false;
    }
    
    return consumed;
}

@end
